package com.shangcheng.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.SmsHomeBrandMapper;
import com.shangcheng.model.domain.SmsHomeBrand;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.security.auth.login.Configuration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/12 15:35
 */
@DubboService
public class SmsHomeBrandApiImpl implements SmsHomeBrandApi{
    @Autowired
    private SmsHomeBrandMapper smsHomeBrandMapper;
    @Override
    public IPage<SmsHomeBrand> findByCondition(Integer pageNum, Integer pageSize, String brandName, Integer recommendStatus) {
        IPage<SmsHomeBrand> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeBrand> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(brandName),SmsHomeBrand::getBrandName,brandName);
        wrapper.eq(!ObjectUtils.isEmpty(recommendStatus),SmsHomeBrand::getRecommendStatus,recommendStatus);
        return smsHomeBrandMapper.selectPage(page,wrapper);
    }

    /**
     * 修改推荐品牌排序
     * @param id id
     * @param sort sort
     */
    @Override
    public void updateSort(Integer id, Integer sort) {
        SmsHomeBrand brand=new SmsHomeBrand();
        brand.setId(Convert.toLong(id));
        brand.setSort(sort);
        LambdaUpdateWrapper<SmsHomeBrand> wrapper= new LambdaUpdateWrapper<>();
        wrapper.eq(SmsHomeBrand::getId,brand.getId());
        wrapper.set(!ObjectUtils.isEmpty(brand.getSort()),SmsHomeBrand::getSort,brand.getSort());
       smsHomeBrandMapper.update(brand,wrapper);
    }

    /**
     * 批量删除推荐品牌列表
     * @param ids 品牌编号
     */
    @Override
    public void deleteByIds(Integer[] ids) {
        LambdaQueryWrapper<SmsHomeBrand> wrapper= new LambdaQueryWrapper<>();
        smsHomeBrandMapper.deleteBatchIds(Arrays.asList(ids.clone()));
    }

    /**
     * 批量修改推荐状态
     * @param ids ids
     * @param recommendStatus 推荐状态
     */
    @Override
    public void updateRecommendStatus(Integer[] ids, Integer recommendStatus) {
        for (Integer id : ids) {
            SmsHomeBrand brand = new SmsHomeBrand();
            brand.setId(Convert.toLong(id));
            LambdaUpdateWrapper<SmsHomeBrand> wrapper= new LambdaUpdateWrapper<>();
            wrapper.eq(SmsHomeBrand::getId,id);
            wrapper.set(SmsHomeBrand::getRecommendStatus,recommendStatus);
            smsHomeBrandMapper.update(brand,wrapper);
        }
    }

    /**
     * 批量添加推荐品牌
     * @param homeBrandList 推荐品牌集合
     */
    @Override
    public void create(List<SmsHomeBrand> homeBrandList) {
        for (SmsHomeBrand brand : homeBrandList) {
            smsHomeBrandMapper.insert(brand);
        }
    }

    /**
     * 根据名称查询推荐品牌
     * @param homeBrandList
     * @return
     */
    @Override
    public List<SmsHomeBrand> findByBrandName(List<SmsHomeBrand> homeBrandList) {
        List<String> brandNames = CollUtil.getFieldValues(homeBrandList, "brandName", String.class);
        LambdaQueryWrapper<SmsHomeBrand> wrapper= new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeBrand::getBrandName,brandNames);
        return smsHomeBrandMapper.selectList(wrapper);
    }

}
